<?php
namespace app\index\controller;


use app\index\BaseController;
use app\index\model\charge\ChargingStation;
use app\index\model\common\Advert;
use app\index\model\common\SystemConfig;
use app\index\model\CurrencyRecord;
use app\index\model\user\UserCard;
use app\index\model\user\UserStationCollection;
use app\worker\DataService;
use app\worker\Events;
use think\facade\Db;
use think\facade\Log;
use think\App;
use think\facade\Request;
use think\facade\View;
use app\index\model\user\User;
use app\index\model\common\Menu;
use app\index\model\user\UserCar;



class Index extends BaseController
{

    public function index()
    {

        dd(22);
        /*
        $pileInfo = Db::table('jy_charge_pile')->where('id',2)->find();
        $clientWhere = ['pile_id'=>2,'is_delete'=>1];
        $pileClientInfo = Db::table('jy_pile_client')->where($clientWhere)->find();
        //$sendData['pile_gun']['num'] = '01';
        $sendData['mode_info'] = Db::table('jy_charge_pile_cost')->where('id',3)->find();
        Events::sendMsg($pileInfo,$pileClientInfo,$sendData,'setPileBillingMode');
        dd(11);
        */
        //return $this->success('获取成功');
        $dataService = new DataService();


        //dump('50460E120F0C14');
        //dump($dataService->cP56TimeToFormatTime('50460E120F0C14'));
        //dump($dataService->formatTimeToCP56Time("2020-12-15 18:14:18"));die;
        /*
        $str = '000000025503141278230500';
        //$str = '0680B10300000002';
        //$s = pack('H*', $str);
        $t = $dataService->modBusCRC($str);
        $crc = strtoupper(unpack("H*",$t)[1]);
        var_dump($crc);
        $t2 = $dataService->src16($str);
        $crc2 = strtoupper(unpack("H*",$t2)[1]);
        var_dump($crc2);
        $str2 = 'DA4C';
        var_dump($str2);
        */
        $ventsService = new Events();
        $ventsService->onWorkerStart('3213415sdfsd');
        $clientId = '3213415sdfsd';
        $pileNumber = '55031412782305';
        //登录
        //$str = '6822033a00018767000000000900010f56312e302e3030000101010101010101010101041d96';
        //心跳包
        //$str = '680D00010003'.$pileNumber.'0100600D';
        //$str = '6840001200130000000000000000000000000000000087670000000009010200010000000000000000000000000000000000000000000000000000000000000000003e93';
        //计费模型验证
        //$str = '680D00020005'.$pileNumber.'00014446';
        //请求计费模式
        //$str = '680B02000009'.$pileNumber.'51a4';
        //实时上传数据
        //$str = '68401A03001310016836029168942993142667612080'.$pileNumber.'01030101020000000000000000000000000000000000000000000000000000000000000000b484';
        //充电握手
        //$str = '684D0015001510016836029168942993142667612080'.$pileNumber.'0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d3a5';
        //参数配置
        //$str = '68310016001710016836029168942993142667612080'.$pileNumber.'0100000000000000000000000000000000000000000049da';
        //充电结束
        //$str = '682B0016001910016836029168942993142667612080'.$pileNumber.'010000000000000000000000000000003ef3';
        //错误报文
        //$str = '68240017001B10016836029168942993142667612080'.$pileNumber.'00000000000000000db3';
        //充电阶段 BMS 中止
        //$str = '68200018001D10016836029168942993142667612080'.$pileNumber.'010000000047F4';
        //充电阶段充电机中止
        //$str = '68200018002110016836029168942993142667612080'.$pileNumber.'01000000006af4';
        //充电过程 BMS 需求与充电机输出
        //$str = '68300019002310016836029168942993142667612080'.$pileNumber.'0122070038012217003500005001002257003530260961f';
        //充电过程 BMS 信息
        //$str = '68230021002510016836029168942993142667612080'.$pileNumber.'0100122412250915cec4';
        //充电桩主动申请启动充电
        //$str = '683700040031'.$pileNumber.'0103002023050913420110000000000000000000000000000000002023050913420110202305091342011011ddcd';
        //远程启动充电命令回复
        //$str = '';
        //远程停机命令回复
        //$str = '6840001500131001690797703093577402844090314087670000000001010300010609000000000000000000000000000000000000000000000000000000000000005628681e000000331001690797703093577402844090314087670000000001010100b1b268a20003003b100169079770309357740284409031408767000000000101d85903121f0717d85903121f071720f19b260000000000000000000000007001d1ab000000000000000000000000f303060600000000000000000000000006060606000000000000000000000000b4c4040000b4c4040000000000000000000000000000000000000000000000000000000000000001d85903121f07176e2307021346062524f0ea';
        //交易记录
        //$str = '680E00030035'.$pileNumber.'01010072ea';
        $str = '68a2000c003b10016912853588249166029870532050876700000000010128231f09060817e8801f0a060817982e0200000000000000000000000000982e020000000000000000000000000068bf0000000000000000000000000000d8d6000084030000840300002c010000c8140500004c1805000084030000840300002c010000000000000000000000000000000000000001e8801f0a0608174123070213460625241715';
        //$str = '680b00030009876700000000015e08';
        $ventsService->onMessage($clientId,$str);
        //$ventsService->onClose($clientId);
        //$ventsService->testInfo();





        //return View::fetch('index/index');
    }

    /**
     * 获取首页内容
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function indexPageInfo(){
        //获取轮播图广告
        $advertModel = new Advert();
        $field = 'id,image,url,create_time';
        $advertList = $advertModel->getListByWhere(['platform_id'=>2,'position_id'=>2],1,20,$field);
        $backData = ['advert_list'=>$advertList];
        return $this->success('获取内容成功',$backData);
    }

    /**
     * 获取首页站点信息
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getChargeStation(){
        $getData = Request::post();
        [$page,$limit] = $this->getPage();
        $chargingStationModel = new ChargingStation();
        if(!isset($getData['page'])) $getData['page'] = 1;
        if(!isset($getData['limit'])) $getData['limit'] = 15;
        $userId = $this->getLogUserId();
        if(isset($getData['type']) && $getData['type'] == 1){
            if($userId){
                $getData['user_id'] = $userId;
                $field = "s.id,s.title,s.icon_img,s.longitude,s.latitude,s.max_power,s.station_remark,s.remark,s.parking_remark,s.is_open,u.station_id";
                $getList = $chargingStationModel->myCollectionStation($getData,$page,$limit,$field);
                foreach ($getList['list'] as $key=>$value){
                    $getList['list'][$key]['is_collect'] = 1;
                }
            }else{
                return $this->fail('请登录',-1);
            }
        }else{
            $field = "id,title,icon_img,longitude,latitude,max_power,station_remark,remark,parking_remark,is_open";
            $getList = $chargingStationModel->getList($getData,$page,$limit,$field);
            if($userId){
                $userStationCollectionModel = new UserStationCollection();
                foreach ($getList['list'] as $key=>$value){
                    $collectionWhere = ['station_id'=>$value['id'],'user_id'=>$userId];
                    $getCollectionResult = $userStationCollectionModel->getInfoByWhere($collectionWhere);
                    $getList['list'][$key]['is_collect'] = $getCollectionResult ? 1 : 0;
                }
            }else{
                foreach ($getList['list'] as $key=>$value){
                    $getList['list'][$key]['is_collect'] = 0;
                }
            }
        }
        return $this->success('获取内容成功',$getList);
    }

    /**
     * 搜索站点
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function findStationInfo(){
        $getData = $this->request->param();
        [$page,$limit] = $this->getPage();
        $chargingStationModel = new ChargingStation();
        if(!isset($getData['page'])) $getData['page'] = 1;
        if(!isset($getData['limit'])) $getData['limit'] = 15;
        if(!isset($getData['title'])) $getData['title'] = '';
        $getData['address'] = $getData['title'];
        $getData['select_status'] = 'or';
        $userId = $this->getLogUserId();
        $field = "id,title,icon_img,longitude,latitude,max_power,station_remark,remark,parking_remark,is_open";
        $getList = $chargingStationModel->getList($getData,$page,$limit,$field);
        if($userId){
            $userStationCollectionModel = new UserStationCollection();
            foreach ($getList['list'] as $key=>$value){
                $collectionWhere = ['station_id'=>$value['id'],'user_id'=>$userId];
                $getCollectionResult = $userStationCollectionModel->getInfoByWhere($collectionWhere);
                $getList['list'][$key]['is_collect'] = $getCollectionResult ? 1 : 0;
            }
        }else{
            foreach ($getList['list'] as $key=>$value){
                $getList['list'][$key]['is_collect'] = 0;
            }
        }
        return $this->success('获取内容成功',$getList);
    }

    //充电状态页面
    function userCarStatus(){

    }

    //会员页面
    function memberInfo(){

    }

    /**
     * 用户中心菜单栏
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function userCenterInfo(){
        //获取用户信息
        $userId = $this->request->uid();
        $userModel = new User();
        $field = 'id,account,status,nickname,avatar,now_money,pay_pwd,store_type';
        $userInfo = $userModel->UserInfoById($userId,$field);
        if($userInfo['status'] == 0){
            return $this->fail('用户被禁用，请联系客服');
        }
        $userInfo['user_coupon'] = 0;//优惠卷
        $userInfo['carbon_credit'] = 0;//碳积分
        $userInfo['user_pay_pwd'] = false;
        if($userInfo['pay_pwd']) $userInfo['user_pay_pwd'] = true;
        $backData = [];
        $backData['user_info'] = $userInfo;
        //获取菜单栏
        $menuModel = new Menu();
        $backData['menu_list'] = $menuModel->getUserCenterMenu(['menu_type'=>1]);
        //我的频道
        $backData['my_menu'] = $menuModel->getUserCenterMenu(['menu_type'=>2]);
        //获取我的车辆
        $userCarModel = new UserCar();
        $carFile = 'id,car_number,is_authentication,car_model';
        $backData['my_car'] = $userCarModel->getUserCar($userId,$carFile);
        //获取中间位置广告
        $advertModel = new Advert();
        $field = 'id,image,url,create_time';
        $backData['advert_info'] = $advertModel->getInfoByWhere(['platform_id'=>2,'position_id'=>3],$field);
        $backData['sign_info'] = '签到+2';
        $backData['reward_info'] = '快电值392，差108领37元礼包';
        $backData['reward_url'] = '';
        $systemConfigModel = new SystemConfig();
        $backData['show_welfare'] = $systemConfigModel->getValueByKey('user_center_welfare_set');//1表示显示福利，0表示不显示福利

        return $this->success('获取内容成功',$backData);
    }




    /**
     * 上传凭证
     * @return Index
     */
    function uploadCurrencyInfo(){
        return $this->uploadsImg("currency");
    }

    /**
     * 多图上传
     * @param $fieldName
     * @return $this
     */
    function uploadsImg($fieldName){
        $file = $this->request->file("file");
        $dirDay = date("Y")."/".date("m");
        $fieldName = \think\facade\Filesystem::disk('index')->putFile("/image/".$fieldName."/".$dirDay,$file);
        if($fieldName){
            return $this->success('上传成功',"/upload/".$fieldName);
        }else{
            return $this->fail('上传失败');
        }
    }
    function dianzhuang(){
        $userId = $this->request->uid();
        $list = Db::table('jy_community2')->where('uid', $userId)->where('status', 1)->select()->toArray();
        if(!empty($list)){
            return $this->success(1,'已有电桩');
        }else{
            return $this->fail(0,'无电桩');
        }
    }

}


